#include "mousemovabledialog.h"
#include <QDebug>

MouseMovableDialog::MouseMovableDialog(QWidget *parent): QDialog(parent)
{
  setModal(true);
  setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
  setMouseTracking(true);

  quitButton = new QPushButton("");
  quitButton->setIcon(QIcon(":/ui/icons/back"));
  connect(quitButton, &QPushButton::clicked, this, &MouseMovableDialog::close);
}


void MouseMovableDialog::setWindowMouseOffset(QPoint value)
{
  windowMouseOffset.setX(value.x());
  windowMouseOffset.setY(value.y());
}

void MouseMovableDialog::mousePressEvent(QMouseEvent *event)
{
  if (event->buttons() == Qt::LeftButton) {
    setWindowMouseOffset(
      event->globalPos() - this->pos()
    );
  }
}

void MouseMovableDialog::mouseMoveEvent(QMouseEvent *event)
{
  if (event->buttons() == Qt::LeftButton) {
    QPoint delta = event->globalPos() - this->windowMouseOffset;
    this->move(delta.x(), delta.y());
  }
}

void MouseMovableDialog::closeEvent(QCloseEvent *event)
{
  this->deleteLater();
  event->accept();
}

void MouseMovableDialog::setupQuitButton()
{
  const int delta = 10;
  const int button_width = 25;
  const int button_height = 19;
  int x = this->width() - button_width - delta;
  int y = delta;
  quitButton->setGeometry(x, y, button_width, button_height);
  quitButton->setParent(this);
  quitButton->setFocusPolicy(Qt::NoFocus);
}

